package net.xi.dmhome.controller;

import com.baomidou.mybatisplus.extension.api.R;
import net.xi.dmhome.Utils.DesAesUtil;
import net.xi.dmhome.entity.User;
import net.xi.dmhome.handler.Result;
import net.xi.dmhome.mapper.UserMapper;
import net.xi.dmhome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Email;

/**
 * @Author: jxhu
 * @Description:用户信息控制层
 * @Date Created in 2021-02-06 15:59
 * @Modified By:
 */

@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserService userService;

    @PostMapping("/user/add")
    public Result add(@Validated @RequestBody(required = false) User user) throws Exception {

        /* @Description: 得到的密码先进行加密访问数据库,@Param:  */
        String password = DesAesUtil.desaes(user.getPassword());

        //判断数据库是否存在相同名字的用户
        String name = user.getUsername();
        String email = user.getEmail();
        User user1 = userMapper.selectByUsername(name);
        User user2 = userMapper.selectByEmail(email);

        if (user2 != null){
            return Result.fail("你的邮箱已经被注册！");
        }
        if (user1 != null) {
            return Result.fail("你的用户名已经被注册！");
        } else {
            user.setPassword(password);
            user.setAvatar("https://www.gdpu.edu.cn/images/xh.png");
            int insert = userMapper.insert(user);
            if (insert != 0) {

                System.out.println("添加成功");
                return Result.success(200, "注册成功", insert);
            } else {
                return Result.fail("注册失败");
            }
        }
    }
    @GetMapping("/user/findbyid/{uuid}")
    public Result getUserById(@PathVariable("uuid") String UUid) throws Exception {
        User user = userMapper.selectByUserUUid(UUid);

        /* @Description: 解码显示密码,@Param:  */
        String password = DesAesUtil.decryptDES(user.getPassword());
        user.setPassword(password);
        return Result.success(user);

    }

    @PutMapping("/user/edit")
    public Result editUser(@Validated @RequestBody User user) throws Exception {
        /* @Description: 得到的密码先进行加密访问数据库,@Param:  */
        String password = DesAesUtil.desaes(user.getPassword());

        //判断数据库是否存在相同名字的用户
        String name = user.getUsername();
        String email = user.getEmail();
        User user1 = userMapper.selectByUsername(name);
        User user2 = userMapper.selectByEmail(email);
        if (user2 != null){
            return Result.fail("你的邮箱已经被注册！");
        }
        if (user1 != null) {
            return Result.fail("你的用户名已经被注册！");
        } else {
            user.setPassword(password);
            boolean update = userService.saveOrUpdate(user);
            if (update !=false) {

                System.out.println("更改成功");
                return Result.success(200, "success", name);
            } else {
                return Result.fail("更改失败");
            }
        }
    }
    @DeleteMapping("/user/delete/{id}")
    public Result deleteUser(@PathVariable("id") Integer id){
        int i = userMapper.deleteById(id);
        if (i == 1){
            return Result.success("success");
        }else {
            return Result.fail("删除失败");
        }
    }


}
